define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template', 'bootstrap-daterangepicker'], function ($, undefined, Backend, Table, Form, Template, daterangepicker) {

    var Controller = {
        detail: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    detail_url: 'exam/student/detail/ids/' + Fast.api.query('ids') + location.search,
                }
            });

            var table = $("#table");
            var columns = [
                [
                    {checkbox: true, rowspan: 2},
                    {field: 'student_name', title: __('姓名'), rowspan: 2},
                    {field: 'student_number', title: __('学号'), rowspan: 2},
                    {field: 'grade_name', title: __('年级'), rowspan: 2, operate:false},
                    {field: 'class_name', title: __('班级'), rowspan: 2, operate:false},
                    {
                        field: 'class_id', title: __('年级/班级'), searchList: function (column) {
                            return Template('classesTpl', {});
                        }, formatter: function (value, row, index) {
                            return '无';
                        }, rowspan: 2, visible: false
                    },
                    {title: '总分', colspan: 3}
                ],
                [
                    {field: 'score', title: '总分'},
                    {field: 'school_sort', title: '总分学校排名'},
                    {field: 'classes_sort', title: '总分班级排名'},
                ]
            ];
            
            if (subjects) {
                $.each(subjects, function (index, data) {
                    if (index > 0) {
                        columns[0].push(
                            {title: data.name, colspan: 3, operate:false}
                        );
                        columns[1].push(
                            {
                                field: 'json', title: data.name + '分数', formatter: function (value, row) {
                                    return value[index].score;
                                }, operate:false
                            },
                            {field: 'json', title: data.name + '学校排名', formatter: function (value, row) {
                                    return value[index].school_sort;
                                }, operate:false
                            },
                            {field: 'json', title: data.name + '班级排名', formatter: function (value, row) {
                                    return value[index].classes_sort;
                                }, operate:false
                            }
                        );
                    }
                });
            }


            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.detail_url,
                pk: 'id',
                sortName: 'id',
                columns: columns,
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        api: {
            bindevent: function () {
                $('.daterange').daterangepicker({
                    timePicker: false,
                    autoUpdateInput: false,
                    timePickerSeconds: true,
                    timePicker24Hour: true,
                    autoApply: true,
                    locale: {
                        format: 'YYYY-MM-DD HH:mm:ss',
                        customRangeLabel: __("Custom Range"),
                        applyLabel: __("Apply"),
                        cancelLabel: __("Clear"),
                    },
                }, function (start, end) {
                    $(this.element).val(start.format(this.locale.format) + " - " + end.format(this.locale.format));
                    $(this.element).trigger('blur');
                });
                Form.api.bindevent($("form[role=form]"));
            },
        }
    };
    return Controller;
});